package com.estar.platform.caseInfo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;

import com.estar.workparent.controller.TmblWorktimeParentController;

public class FileOperationUtil {
	
	private static final Logger logger = Logger.getLogger(FileOperationUtil.class);
	
	// 文件重命名
	public static boolean fileRename(String filePath, String oldName,
			String newName) {

		boolean result = false;
		if (!oldName.equals(newName)) { // 新的文件名和以前文件名不同时,才有必要进行重命名
			File oldfile = new File(filePath + "/" + oldName);
			File newfile = new File(filePath + "/" + newName);
			if (!oldfile.exists()) {
				System.out.println("旧文件夹不存在！");
			} else {
				if (newfile.exists()) {
					System.out.println("新文件名已存在！");
				} else {
					result = oldfile.renameTo(newfile);
				}
			}
		} else {
			System.out.println("新文件名和旧文件名相同！");
		}
		return result;
	}

	// 文件删除
	public static void deleteFile(String sPath) {
		File file = new File(sPath);
		if (file.isFile() && file.exists()) {
			file.delete();
		}
	}

	// 文件夹复制
	public static void copyDir(String oldPath, String newPath)
			throws IOException {
		File file = new File(oldPath);
		String[] filePath = file.list();

		if (!(new File(newPath)).exists()) {
			(new File(newPath)).mkdir();
		}

		for (int i = 0; i < filePath.length; i++) {
			if ((new File(oldPath + File.separator + filePath[i]))
					.isDirectory()) {
				copyDir(oldPath + File.separator + filePath[i], newPath
						+ File.separator + filePath[i]);
			}

			if (new File(oldPath + File.separator + filePath[i]).isFile()) {
				copyFile(oldPath + File.separator + filePath[i], newPath
						+ File.separator + filePath[i]);
			}

		}
	}

	// 文件复制
	public static void copyFile(String oldPath, String newPath)
			 {
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			File oldFile = new File(oldPath);
			if(oldFile.exists()){
				File file = new File(newPath);
				
					in = new FileInputStream(oldFile);
					out = new FileOutputStream(file);
		
					byte[] buffer = new byte[1024];
		
					while ((in.read(buffer)) != -1) {
						out.write(buffer);
					}
					if(in!=null){
						in.close();	
					}
					if(out!=null){
						out.flush();
						out.close();
					}
				
			}
		} catch (Exception e) {
			logger.error("文件复制错误：", e);
			e.printStackTrace();
		} finally {
			try {
					if(in!=null){
				
					in.close();
					}
					if(out!=null){
						out.flush();
						out.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
			
		}

	

	//文件夹删除
	public static void deleteAllFilesOfDir(File path) {  
	    if (!path.exists())  
	        return;  
	    if (path.isFile()) {  
	    	path.delete(); 
	        return;  
	    }  
	    File[] files = path.listFiles();  
	    for (int i = 0; i < files.length; i++) {  
	        deleteAllFilesOfDir(files[i]);  
	    }  
	    path.delete();  
	}
	
	public static void main(String[] args) throws IOException {
		File file = new File("D:/备份/estar-mgr/appPicture/20161111195956/粤AMXMQM/2016-11-11");
		deleteAllFilesOfDir(file);
	}
}
